package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ICompetitionRelevanceService;
import com.mdd.admin.validate.CompetitionRelevanceCreateValidate;
import com.mdd.admin.validate.CompetitionRelevanceUpdateValidate;
import com.mdd.admin.validate.CompetitionRelevanceSearchValidate;
import com.mdd.admin.vo.CompetitionRelevanceListedVo;
import com.mdd.admin.vo.CompetitionRelevanceDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.CompetitionRelevance;
import com.mdd.common.mapper.CompetitionRelevanceMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 竞业关联实现类
 * @author LikeAdmin
 */
@Service
public class CompetitionRelevanceServiceImpl implements ICompetitionRelevanceService {
        
    @Resource
    CompetitionRelevanceMapper competitionRelevanceMapper;

    /**
     * 竞业关联列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CompetitionRelevanceListedVo>
     */
    @Override
    public PageResult<CompetitionRelevanceListedVo> list(PageValidate pageValidate, CompetitionRelevanceSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<CompetitionRelevance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        competitionRelevanceMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:blacklistedCustomer@blacklisted_customer:str",
        });

        IPage<CompetitionRelevance> iPage = competitionRelevanceMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CompetitionRelevanceListedVo> list = new LinkedList<>();
        for(CompetitionRelevance item : iPage.getRecords()) {
            CompetitionRelevanceListedVo vo = new CompetitionRelevanceListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 竞业关联详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return CompetitionRelevance
     */
    @Override
    public CompetitionRelevanceDetailVo detail(Integer id) {
        CompetitionRelevance model = competitionRelevanceMapper.selectOne(
                new QueryWrapper<CompetitionRelevance>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CompetitionRelevanceDetailVo vo = new CompetitionRelevanceDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 竞业关联新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(CompetitionRelevanceCreateValidate createValidate) {
        CompetitionRelevance model = new CompetitionRelevance();
        model.setBlacklistedCustomerId(createValidate.getBlacklistedCustomerId());
        model.setBlacklistedCustomer(createValidate.getBlacklistedCustomer());
        model.setBlacklistedNelvolt(createValidate.getBlacklistedNelvolt());
        model.setBlacklistedTypeId(createValidate.getBlacklistedTypeId());
        model.setBlacklistedType(createValidate.getBlacklistedType());
        model.setConflictInterestClientSources(createValidate.getConflictInterestClientSources());
        model.setRestrictionsBranchCompanies(createValidate.getRestrictionsBranchCompanies());
        model.setConflictInterestRequirement(createValidate.getConflictInterestRequirement());
        model.setSource(createValidate.getSource());
        model.setIsShow(createValidate.getIsShow());
        model.setCreatesUserId(createValidate.getCreatesUserId());
        model.setCreatesUser(createValidate.getCreatesUser());
        model.setCreatesTime(createValidate.getCreatesTime());
        model.setUpdatesUserId(createValidate.getUpdatesUserId());
        model.setUpdatesUser(createValidate.getUpdatesUser());
        competitionRelevanceMapper.insert(model);
    }

    /**
     * 竞业关联编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(CompetitionRelevanceUpdateValidate updateValidate) {
        CompetitionRelevance model = competitionRelevanceMapper.selectOne(
                new QueryWrapper<CompetitionRelevance>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setBlacklistedCustomer(updateValidate.getBlacklistedCustomer());
        model.setBlacklistedNelvolt(updateValidate.getBlacklistedNelvolt());
        model.setBlacklistedType(updateValidate.getBlacklistedType());
        model.setConflictInterestClientSources(updateValidate.getConflictInterestClientSources());
        model.setRestrictionsBranchCompanies(updateValidate.getRestrictionsBranchCompanies());
        model.setConflictInterestRequirement(updateValidate.getConflictInterestRequirement());
        model.setSource(updateValidate.getSource());
        model.setIsShow(updateValidate.getIsShow());
        model.setCreatesUser(updateValidate.getCreatesUser());
        model.setCreatesTime(updateValidate.getCreatesTime());
        model.setUpdatesUser(updateValidate.getUpdatesUser());
        competitionRelevanceMapper.updateById(model);
    }

    /**
     * 竞业关联删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        CompetitionRelevance model = competitionRelevanceMapper.selectOne(
                new QueryWrapper<CompetitionRelevance>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        competitionRelevanceMapper.updateById(model);
    }

}
